<?php 
	class LoginWiget extends CWidget 
	{
		function run() 
		{
			$model=new LoginForm;
					// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
			{	
				if(Yii::app()->user->role=="admin")
				{
					header('Location: /admin/qmain/');
				}
				else 
				{	
					header('Location: /userpage/');
				}
			}
		}
		// display the login form			
			$this->render('login',array('model'=>$model));
		}
	}

?>